package com.gzedu.module.controller.curd;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gzedu.common.db.DBResult;
import com.gzedu.common.sys.controller.BaseController;
import com.gzedu.module.faced.curd.CurdListFacade;
import com.gzedu.module.modle.curd.CurdList;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("/curdList")
public class CurdListController extends BaseController{
	@Autowired
	private CurdListFacade curdListFacade;
	
	/**
	 * 获取字段名称
	 * @param request
	 * @param response
	 * @param model
	 * @param sql
	 * @return
	 */
	@RequestMapping("/getColInfo")
	@ResponseBody
	public String getColInfo(HttpServletRequest request, HttpServletResponse response, Model model,String sql){
		List<String> col = DBResult.getColInfo(sql);
		JSONArray json = JSONArray.fromObject(col);
		return json.toString();
	}
	
	/**
	 * 获取通用列表列设置信息
	 * @param request
	 * @param response
	 * @param model
	 * @param curdList
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/getTabColInfo")
	public String getTabColInfo(HttpServletRequest request, HttpServletResponse response, Model model,CurdList curdList,String sql,String tabSpanId,String val) throws UnsupportedEncodingException{
		if(!"".equals(tabSpanId) && "".equals(val)){
			curdList.setCurd_list_id(tabSpanId);
			List<CurdList> res = curdListFacade.getByListId(curdList);
			if(res != null && res.size() > 0){
				for(CurdList curdLists : res){
					model.addAttribute("curdListId", curdLists.getCurd_list_id());
					model.addAttribute("tabColInfo", curdLists.getList_column());
					model.addAttribute("filedName",curdLists.getFiled_name());
					model.addAttribute("pos",curdLists.getPosition());
				}
			}
		}
		if(!"".equals(val) && null != val){
			String str[] = val.split(",");
			model.addAttribute("curdListId", str[0]);
			model.addAttribute("tabColInfo", str[1]);
			model.addAttribute("colName", new String(str[2].getBytes("iso-8859-1"),"UTF-8"));
			model.addAttribute("pos",str[3]);
		}
		model.addAttribute("sql",sql);
		model.addAttribute("tabSpanId", tabSpanId);
		return "/views/iframePop/tabPop";
	}
	
	
	
}
